/*
 * @Author: 锦阳
 * @Create: 2021年04月22日
 */

const doc = require('jsdoc-to-markdown');
const dmd = require('dmd');
const DmdOptions = require('jsdoc-to-markdown/lib/dmd-options');
/**
 *  渲染jsdoc文档
 *
 * @param {Jsdoc2mdOptions} options jsdocToMarkdown配置参数
 * @returns {Promise<string>} 异步返回jsdoc文档文本
 * @ignore
 */
function jsdocRender(options) {
    // return doc.render(options);
    /* istanbul ignore next */
    options = options || {};
    options.dataModify = options.dataModify || (val => val);
    const dmdOptions = new DmdOptions(options);
    /* istanbul ignore next */
    if (options.data) {
        return dmd.async(options.dataModify(options.data), dmdOptions);
    } else {
        return doc.getTemplateData(options)
            .then(templateData => dmd.async(options.dataModify(templateData), dmdOptions));
    }
}

module.exports = jsdocRender;

/**
 * jsdocToMarkdown配置参数，具体可[👉参考文档](https://github.com/jsdoc2md/jsdoc-to-markdown/blob/master/docs/API.md)
 *
 * @typedef {import('jsdoc-to-markdown').JsdocOptions|import('jsdoc-to-markdown').RenderOptions|DataModifySupport} Jsdoc2mdOptions
 * @memberof GenDoc.render
 * @alias Jsdoc2mdOptions
 */

/**
 *  在原有Jsdoc2mdOptions基础上扩展dataModify属性，支持修改templateData
 *
 * @typedef {object} DataModifySupport
 * @property {DataModify} dataModify
 * @memberof GenDoc.render
 * @alias DataModifySupport
 */

/**
 * @callback DataModify
 * @param {object[]} doclets
 * @returns {object[]}
 * @memberof GenDoc.render
 * @alias DataModify
 */
